/*
 * main.cpp
 *
 *  Created on: 10/01/2011
 *      Author: Nenem
 */
#include "ObjectManager.h"
#include "Input.h"
#include "Graphics.h"

#include "AttackState.h"
#include "IdleState.h"
#include "WalkState.h"
#include "RunState.h"
#include "HittedState.h"

#include "Dictionary.h"
#include "Scenery.h"

#include <windows.h>
#include <iostream>
using namespace std;

int next_game_tick;
int FRAMES_PER_SECOND = 60;
int SKIP_TICKS = 1000 / FRAMES_PER_SECOND;

int onSleep();

int main( int argc, char *argv[] ){
	//teste
	ObjectManager *objManager= new ObjectManager;
	Scenery *scenery = new Scenery("mapadoido");
	Graphics *graphics;
	GameObject *player;
	Input *input = new Input();

	//SDL
	SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
	IMG_Init(IMG_INIT_PNG);
	atexit(IMG_Quit);
	atexit(SDL_Quit);
	SDL_ShowCursor(SDL_DISABLE);

	next_game_tick = SDL_GetTicks();
	SDL_Event *event = new SDL_Event;
	//
	graphics = new Graphics();

	objManager->createPlayer("Duran");
	cout << "Player Created\n";
	player = objManager->getPlayer();

	scenery->update(0, objManager);
	cout << "Scenery Updated\n";

	int delta = 0;
	int time = SDL_GetTicks();

	while(input->onEvent(event, player->controller)){

		delta = (int)SDL_GetTicks() - time;
		time = SDL_GetTicks();
//		cout << "Delta " << delta << " time: " << time << "\n";
		objManager->update(delta, time, scenery, graphics);
//		cout << "So funciona se tiver esse cout\n";
		scenery->moveMap(player->model->position, graphics->RESOLUTION_X, graphics->RESOLUTION_Y);
		scenery->update(delta, objManager);
		graphics->Draw(scenery->getX(), scenery->getY(), scenery->layerA, scenery->layerB);

		objManager->getConversationView()->update(delta, graphics);

		graphics->updateScreen();

		onSleep();
	}

	return 0;
}

//int main( int argc, char *argv[] ){
//	Graphics *graphics;
//	GameObject *player;
//	Input *input = new Input();
//
//	Model *model = new Model;
//	View *view = new NormalView;
//	Controller *controller = new Controller;
//
//	Collision *collision = new Collision;
//
//	player = new GameObject(1);
//	model->movement = new Movement(50,100);
////	model->collision = new Collision;
//	model->position = new Position(Point2D(50,60));
//	model->rigidBody = new RigidBody(model->position);
//	collision->addCollisioner(model->rigidBody, player->getObjID());
//	model->direction = new Direction();
//	model->battle = new Battle;
//	model->battle->setAttack(new Attack(8));
//	model->battle->setHitted(new Hitted(2));
//	player->model = model;
//	player->view = view;
//	player->controller = controller;
//
//	model->collision = collision;
//
//	player->resource = new ResourceManager();
//	player->resource->readInfo("Duran");
//
//	player->stateAttack = new AttackState;
//	player->stateHitted = new HittedState;
//	player->stateIdle = new IdleState;
//	player->stateWalk = new WalkState;
//	player->stateRun = new RunState;
//
//	player->stateMachine = new StateMachine<GameObject>(player);
//	player->stateMachine->SetCurrentState(player->stateIdle);
//
//
//	//SDL
//	SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
//	SDL_ShowCursor(SDL_DISABLE);
//
//	next_game_tick = SDL_GetTicks();
//	SDL_Event *event = new SDL_Event;
//	//
//	graphics = new Graphics();
//	int delta = 0;
//	player->stateMachine->CurrentState()->Enter(delta, SDL_GetTicks(),player);
//
//	while(input->onEvent(event, player->controller)){
//		player->stateMachine->Update(delta, SDL_GetTicks());
//		player->view->update(delta, player->model, graphics);
//
//		graphics->Draw();
//		graphics->updateScreen();
//		delta = onSleep();
//	}
//
//	SDL_Quit();
//	return 0;
//}

int onSleep(){
	int sleep_time = 0;
	next_game_tick += SKIP_TICKS;
	sleep_time = next_game_tick - SDL_GetTicks();
	if(sleep_time < 0)
		sleep_time = 0;
	if( sleep_time >= 0 ) {
		Sleep( sleep_time );
	}
	else {
		// Shit, we are running behind!
	}
	return sleep_time;
}
